﻿using System;
using System.Collections.Generic;
using ZX.BLL.Extend;
using ZX.CRM.Library;
using ZX.Common;
using System.Data;
using lite;
using Csla.Core;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using OpenExpressApp;
using System.Data.SqlClient;
namespace ZX.CRM.Bussiness
{
    /*
     * 描述：绑定待办事宜条数
     * 新建日期：2012-3-22
     * 修改记录：无
     * 作者：曾洋 
     */
    public class BCrmPayFlow : BaseBussnies
    {
        /// <summary>
        /// 获取缴款单信息
        /// </summary>
        /// <returns></returns>
        public V_CrmPay GetCrmPayInfoById(Guid? workFlowInsId)
        {
            this.entity = new V_CrmPay();
            return GetEntityBySqlComm("select * from V_CrmPay where WorkFlowInsId='" + workFlowInsId + "'") as V_CrmPay;
        }
        public string GetTableInfoById(Guid? worflowinsid, string table)
        {
                IResultSet dt = null;
                object[] dr = null;
                string sql = "select * from " + table + " where WorkFlowInsId='" + worflowinsid + "'";
                using (var db = DBHelper.CreateDb(ZX.DAL.ConnectionStringNames.OpenExpressApp))
                {
                    dt = db.ExecSql(sql);
                    if (dt.Rows > 0) 
                    {
                        dr = dt.GetRow(0);
                    }
                }

                return dr[0].ToString();
        }
        /// <summary>
        /// 获取待办的任务
        /// </summary>
        /// <param name="strLoginName"></param>
        /// <returns></returns>
        public int GetCountUnDone(string strLoginName){
            this.entity= new V_HF_TaskInstanceView();
            IList<V_HF_TaskInstanceView> ilist = GetEntitysBySqlComm<V_HF_TaskInstanceView>("select OperContent from dbo.V_HF_TaskInstanceView where OperContent in (select 'ALL' as OperContent union all select OperContent  from HF_opercontentView where UserId='" + strLoginName + "')");
            return ilist==null?0:ilist.Count;
        }


        /// <summary>
        /// 计算培训类佣金基础表
        /// </summary>
        /// <param name="Orderid">订单产品ID</param>
        /// <param name="adduser">用户ID</param>
        /// <param name="CommisionDate">佣金所属月份</param>
        /// <returns></returns>
        public string ExecCrmCommisionInfo(string Orderid,Guid  adduser,DateTime CommisionDate,string procName)
        {
            string returnstr = "";
            SqlParameter OrderProductId = new SqlParameter("@OrderProductId", SqlDbType.NVarChar, 50);
            OrderProductId.Direction = ParameterDirection.Input;
            OrderProductId.Value = Orderid;
            SqlParameter AddUsers = new SqlParameter("@AddUser", SqlDbType.UniqueIdentifier, 50);
            AddUsers.Direction = ParameterDirection.Input;
            AddUsers.Value = adduser;
            SqlParameter Attachdate = new SqlParameter("@Attachdate", SqlDbType.DateTime, 50);
            Attachdate.Direction = ParameterDirection.Input;
            Attachdate.Value = CommisionDate;
            SqlParameter ReturnValue = new SqlParameter("@ReturnValue", SqlDbType.NVarChar, 50);
            ReturnValue.Direction = ParameterDirection.Output;
            IDataParameter[] parameters = new IDataParameter[] { OrderProductId, Attachdate, AddUsers, ReturnValue };
            int IResult = 0;
            ZX.DAL.DBExtend.DbSql ExecPro = new DAL.DBExtend.DbSql();
            returnstr = ExecPro.RunProcedureReturnString(procName, parameters, out IResult);
            return returnstr;
        }
        /// <summary>
        /// 计算培训类佣金基础表
        /// </summary>
        /// <param name="Orderid">订单产品ID</param>
        /// <param name="adduser">用户ID</param>
        /// <param name="CommisionDate">佣金所属月份</param>
        /// <returns></returns>
        public string ExecCrmCommisionInfo(string Orderid, Guid adduser, DateTime CommisionDate)
        {
            string returnstr = "";
            SqlParameter OrderProductId = new SqlParameter("@OrderProductId", SqlDbType.NVarChar, 50);
            OrderProductId.Direction = ParameterDirection.Input;
            OrderProductId.Value = Orderid;
            SqlParameter AddUsers = new SqlParameter("@AddUser", SqlDbType.UniqueIdentifier, 50);
            AddUsers.Direction = ParameterDirection.Input;
            AddUsers.Value = adduser;
            SqlParameter Attachdate = new SqlParameter("@Attachdate", SqlDbType.DateTime, 50);
            Attachdate.Direction = ParameterDirection.Input;
            Attachdate.Value = CommisionDate;
            SqlParameter ReturnValue = new SqlParameter("@ReturnValue", SqlDbType.NVarChar, 50);
            ReturnValue.Direction = ParameterDirection.Output;
            IDataParameter[] parameters = new IDataParameter[] { OrderProductId, Attachdate, AddUsers, ReturnValue };
            int IResult = 0;
            ZX.DAL.DBExtend.DbSql ExecPro = new DAL.DBExtend.DbSql();
            returnstr = ExecPro.RunProcedureReturnString("Sp_GetCrmComisionExtend", parameters, out IResult);
            return returnstr;
        }      
    }
}
